package com.hqizcc.ltcs.hqbsltcs.service;

import com.hqizcc.ltcs.hqbsltcs.common.Global;
import com.hqizcc.ltcs.hqbsltcs.mapper.GetCityMapper;
import com.hqizcc.ltcs.hqbsltcs.mapper.GetCountyMapper;
import com.hqizcc.ltcs.hqbsltcs.model.City;
import com.hqizcc.ltcs.hqbsltcs.model.County;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class GetCountyServiceImpl implements GetCountyService {

    @Autowired
    private GetCountyMapper getCountyMapper;

    @Autowired
    private GetCityMapper getCityMapper;

    @Override
    public boolean getCounty() {

        List<City> cityList = getCityMapper.findAllCity();

        County county = new County();

        for (City city : cityList){

            String htmlUrl = city.getCityHtml();

            Element body = null;

            try {

                body = Jsoup.connect(htmlUrl).ignoreContentType(true).userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:49.0) Gecko/20100101 Firefox/49.0").timeout(600000).get().body();

                Elements elements = body.select(".countytr");

                for (Element element : elements){

                    Elements td = element.select("td");

                    Elements a = td.select("a");

                    if (a.isEmpty()){
                        String countyIdAndName = td.text();

                        String countyId = countyIdAndName.substring(0,12);

                        String countyName = countyIdAndName.substring(13);

//                        System.out.println(countyId + " " + countyName);

                        String countyUrl = "没有下属的地区";

                        county.setCountyId(countyId);
                        county.setCountyName(new String(countyName.getBytes(), "UTF-8"));
                        county.setCountyHtml(countyUrl);

                        String city_id = getCountyMapper.findCityAndCountyBycountyId(county.getCountyId());

                        String county_name = getCountyMapper.findCountyByCountyId(county.getCountyId());

                        if (city_id == null){

                            getCountyMapper.saveCityAndCounty(city.getCityId(), county.getCountyId());

                        } else {

                            System.out.println(city_id);

                        }

                        if (county_name == null ){

                            getCountyMapper.saveCounty(county.getCountyId(), county.getCountyName(), county.getCountyHtml());

                        } else {

                            System.out.println(county_name);

                        }

                        Thread.sleep(1000);

                    } else {

                        String countyUrl = td.select("a").attr("href");

                        String countyIdAndName = td.select("a").text();

//                        System.out.println(countyIdAndName);

                        String countyId = countyIdAndName.substring(0, 12);

                        String countyName = countyIdAndName.substring(13);

                        String url = countyUrl.substring(countyUrl.indexOf("/")+1);

                        countyUrl = Global.URL + url.substring(0,2) + "/" + countyUrl;

//                        System.out.println(countyId + " " + countyName + " " + countyUrl);
//
                        county.setCountyId(countyId);
                        county.setCountyName(new String(countyName.getBytes(), "UTF-8"));
                        county.setCountyHtml(countyUrl);

                        String city_id = getCountyMapper.findCityAndCountyBycountyId(county.getCountyId());

                        String county_name = getCountyMapper.findCountyByCountyId(county.getCountyId());

                        if (city_id == null){

                            getCountyMapper.saveCityAndCounty(city.getCityId(), county.getCountyId());

                        } else {

                            System.out.println(city_id);

                        }

                        if (county_name == null ){

                            getCountyMapper.saveCounty(county.getCountyId(), county.getCountyName(), county.getCountyHtml());

                        } else {

                            System.out.println(county_name);

                        }

                        Thread.sleep(1000);

                    }

                }

                Thread.sleep(1000*2);

            } catch (IOException e) {

                e.printStackTrace();

                return false;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        return true;
    }
}
